<template>
	<view class="m-f-ac m-f-jsb home-list-item"
	hover-class="home-list-hover"
	@tap="clickevent">
		<view class="m-f-ac"><view v-if="item.icon" class="icon iconfont" :class="['icon-' + item.icon]"></view>{{item.name}}</view>
		<view class="icon iconfont" :class="{'icon-qianjinxiayige': !item.data}">
			<text class="icon-text">{{item.data || ''}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			item: {
				type: Object
			}
		},
		methods: {
			clickevent() {
				switch(this.item.clicktype) {
					case 'navigateTo':
					if (this.item.url) {
						uni.navigateTo({
							url: this.item.url,
						})
					}
						break
					case 'switchTab':
					if (this.item.url) {
						uni.switchTab({
							url: this.item.url,
						})
					}
						break
					case 'clear':
					uni.showModal({
						title: '提示',
						content: '是否要清除缓存？',
						confirmText: '立刻清除',
						success: (res) => {
							if (res.confirm) {
								uni.clearStorage()
								uni.showToast({
									title: '清除缓存成功！'
								})
								this.User.logout(false)
							}
						}
					})
						break
					case 'bind':
					if (this.User.userbind[this.item.provider]) return
					this.bindother()
						break
				}
			}
		},
		bindother() { // 绑定第三方登录
			uni.login({
				provider: this.item.provider,
				// #ifdef MP-ALIPAY
				scopes: 'auth_user', //支付宝小程序需设置授权类型
				// #endif
				success: (res) => {
					uni.getUserInfo({
						provider: this.item.provider,
						success: (infoRes) => {
							this.loginEvent(this.User.__formatOtherLogin(this.item.provider, Object.assign(infoRes, res)))
						}
					})
				},
				fail: (err) => {
					uni.showToast({
						title: '绑定失败',
						icon: 'none'
					})
					console.log('login fail:', err);
				}
			});
		},
		async bindEvent(data) {
			uni.showLoading({ title: '绑定中...', mask: true })
			let [err, res] = await this.$http.post('/user/bindother', data, {
				token: true,
				checkToken: true
			})
			if (!this.$http.errorCheck(err, res)) return uni.hideLoading()
			// 绑定成功
			uni.hideLoading()
			uni.showToast({ title: '绑定成功' })
			// 修改状态，缓存
			this.User.userbind[this.item.provider] = {
				nickname: data.nickname
			}
			uni.setStorageSync('userbind', this.User.userbind)
			this.$emit('updateuserbind')
		},
	}
</script>

<style lang="scss" scoped>
	.home-list-hover {
		background-color: #d6d6d6;
	}
	.home-list-item {
		padding: 20rpx;
		border-bottom: 1rpx solid #EEEEEE;
		.icon-liulan {
			color: #007AFF;
		}
		.icon-huiyuanvip {
			color: #f85200;
		}
		.icon {
			margin-right: 20rpx;
			font-size: 36rpx;
		}
		.icon-text {
			font-size: 30rpx !important;
			color: #CCCCCC;
		}
		.icon-qianjinxiayige {
			color: #CCCCCC;
		}
	}
</style>
